package cn.china.xfood.controller;

import cn.china.xfood.service.LoginService;
import entity.User;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

@RequestMapping("login")
    public  String  login(@RequestParam String username, @RequestParam String password, HttpServletResponse res){
    res.setHeader("Access-Control-Allow-Origin" ,"*");





    if (username!=""&&password!=""){
//        User user = loginService.selectByName(username);
//
//            if (user!=null){
                User user1 = loginService.selectByNameAndPassword(username, password);
                if (user1!=null){
                    return "yes";
                }else {
                    return "用户名或密码错误请重新输入";
                }
//            }else{
//                return "用户名不存在";
//            }

    }else {
        return "用户名或密码不能为空";
    }


}
    @RequestMapping("register")
    public String register( HttpServletResponse res,@RequestParam String username, @RequestParam String password,@RequestParam String phone){
        res.setHeader("Access-Control-Allow-Origin" ,"*");


        String reg1 = "^[A-Za-z0-9]+$";
        String reg2 = "^[\\u4e00-\\u9fa5]+$";






        if (username.matches(reg2)){
            User user = loginService.selectByName(username);

            if (user!=null){
                return "用户名已存在";
            }else {

                if (password.matches(reg1)){
                    int i = loginService.insert(new User(username, password, phone));
                    if (i>0){
                        return "ok";
                    }else {
                        return "网络繁忙......";
                    }
                }else {
                        return "密码只能包含字母和数字";
                }


            }

        }else {
            return "用户名只能包含汉字";
        }




    }


}
